package com.woniu.bank.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.bank.dto.AgreementGroupDTO;
import com.woniu.bank.dto.NewAgreementDTO;
import com.woniu.bank.service.AgreementService;
import com.woniu.bank.util.Result;
import com.woniu.bank.vo.AgreementDefaultDataVO;
import com.woniu.bank.vo.AgreementGroupMemberVO;
import com.woniu.bank.vo.AgreementGroupVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 联保协议的控制层
 *
 * @author ：zhuboyuan,晨星
 * @date ：Created in 2023/1/7 14:34
 */
@RestController
@RequestMapping("agreements")
@Api(tags = "联保协议接口")
public class AgreementController {

    private final AgreementService agreementService;

    public AgreementController(AgreementService agreementService) {
        this.agreementService = agreementService;
    }

    @PostMapping("select")
    @ApiOperation(value = "查询联保协议", notes = "查询联保协议", httpMethod = "POST")
    public Result selectAll(@RequestBody AgreementGroupDTO agreementGroupDTO) {
        List<AgreementGroupVO> agreementList = agreementService.select(agreementGroupDTO);
        PageInfo<AgreementGroupVO> pageInfo = new PageInfo<>(agreementList);
        return Result.ok().data("agreementList", pageInfo);
    }

    @PostMapping("selectGroupMember")
    @ApiOperation(value = "查看联保小组成员", notes = "根据联保协议id查询联保小组成员", httpMethod = "POST")
    public Result selectGroupMember(Integer id) {
        List<AgreementGroupMemberVO> agreementGroupMemberVOList = agreementService.selectGroupMember(id);
        return Result.ok().data("agreementGroupMemberVOList", agreementGroupMemberVOList);
    }

    @PostMapping("returnDefaultData")
    @ApiOperation(value = "联保协议的默认数据", notes = "根据联保协议的id，去查询并返回联保协议需要的默认数据", httpMethod = "POST")
    public Result returnDefaultData(@RequestBody AgreementGroupVO agreement) {
        AgreementDefaultDataVO defaultDataVO = agreementService.returnDefaultData(agreement);
        return Result.ok().data("defaultDataVO", defaultDataVO);
    }

    @PostMapping("insertAgreement")
    @ApiOperation(value = "签订联保协议", notes = "根据联保协议id，签订联保协议", httpMethod = "POST")
    public Result insertAgreement(@RequestBody NewAgreementDTO agreement) {
        if (!"未生效".equals(agreement.getAgreementStatus())){
            return Result.error("只可签订未生效的协议");
        }
        int i = agreementService.insertAgreement(agreement);
        if (i == 1) {
            return Result.ok("协议已签订");
        } else {
            return Result.error("签订失败，请填写完整信息");
        }
    }

    @PostMapping("terminationAgreement")
    @ApiOperation(value = "终止联保协议", notes = "根据联保协议id，终止联保协议。只要协议id，其他不填", httpMethod = "POST")
    public Result terminationAgreement(@RequestBody AgreementGroupVO agreement) {
        if ("终止".equals(agreement.getAgreementStatus())){
            return Result.error("只可终止未签订和正常的协议");
        }
        int i = agreementService.terminationAgreement(agreement);
        if (i == 1) {
            return Result.ok("协议已终止");
        } else {
            return Result.error("终止失败，请联系工作人员");
        }
    }

    @PostMapping("cancellationAgreement")
    @ApiOperation(value = "联保协议作废", notes = "根据联保协议id，联保协议作废", httpMethod = "POST")
    public Result cancellationAgreement(@RequestBody AgreementGroupVO agreement) {
        int i = agreementService.cancellationAgreement(agreement);
        if (i == 1) {
            return Result.ok("协议已作废");
        } else {
            return Result.error("废弃失败，请联系工作人员");
        }
    }

}
